package com.ag.improvedjava.iterable;

import java.util.Comparator;
import java.util.Iterator;

import com.ag.improvedjava.utils.Function;
import com.ag.improvedjava.utils.Grouping;

final class GroupByIterable<K, T, U> implements Iterable<Grouping<K, U>> {

	private final ImprovedIterable<T> iterable;
	private final Function<T, K> keySelector;
	private final Function<T, U> elementSelector;
	private final Comparator<K> comparer;
	
	GroupByIterable(ImprovedIterable<T> iterable, Function<T, K> keySelector, Function<T, U> elementSelector,
			Comparator<K> comparer) {
		this.iterable = iterable;
		this.keySelector = keySelector;
		this.elementSelector = elementSelector;
		this.comparer = comparer;
	}

	@Override
	public Iterator<Grouping<K, U>> iterator() {
		return iterable.toLookup(keySelector, elementSelector, comparer).iterator();
	}
}
